package com.qiankun.tang.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.common.library.listener.OnAppListener;
import com.common.library.ui.activity.BaseActivity;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.PlayerItem;
import com.qiankun.tang.ui.view.adapter.player.PlayerAdapter;
import com.tang.player.beans.MediaBean;
import com.tang.player.ui.view.PlayerWidget2;

import java.util.ArrayList;
import java.util.List;

public class PlayerActivity extends BaseActivity implements OnAppListener.OnInteractionListener {
    private String id;
    private PlayerWidget2 mPlayerWidget;
    private RecyclerView mRecyclerView;
    private PlayerAdapter mPlayerAdapter;
    private GridLayoutManager mManager;

    private List<String> playImgs = new ArrayList<>();
    private List<String> playerUrls = new ArrayList<>();
    private List<String> playerTitles = new ArrayList<>();
    private List<String> playerSubTitles = new ArrayList<>();
    private List<String> playerDates = new ArrayList<>();

    public static Intent newIntent(String id, Context context) {
        Intent i = new Intent(context, PlayerActivity.class);
        i.putExtra("data", id);
        return i;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        id = getIntent().getExtras().getString("data");
        init();
        initData();
        refreshData(id);
    }
    private void init() {
        mPlayerWidget = (PlayerWidget2) findViewById(R.id.activity_player_PlayerWidget);
        mRecyclerView = (RecyclerView) findViewById(R.id.activity_player_RecyclerView);
        mRecyclerView.setLayoutManager(mManager = new GridLayoutManager(this, 1, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mPlayerAdapter = new PlayerAdapter(this, this));
    }
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_zoomin, R.anim.activity_zoomout);
    }
    private void refreshData(String id) {
        this.id = id;

        MediaBean bean = new MediaBean();
        bean.setName("测试名称");
        bean.setUrl(playerUrls.get(Integer.valueOf(id)));
        mPlayerWidget.startPlay(bean);

        if(mPlayerAdapter.getItemCount() != 0){
            mRecyclerView.removeAllViews();
            mPlayerAdapter.setDatas(new ArrayList<PlayerItem>());
            mPlayerAdapter.notifyDataSetChanged();
            mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    List<PlayerItem> items = newItems();
                    mPlayerAdapter.setDatas(items);
                    mPlayerAdapter.notifyDataSetChanged();
                }
            },1000);
        }else {
            List<PlayerItem> items = newItems();
            mPlayerAdapter.setDatas(items);
            mPlayerAdapter.notifyDataSetChanged();
        }
    }

    private List<PlayerItem> newItems() {
        int pos = Integer.valueOf(id);
        List<PlayerItem> items = new ArrayList<>();

        //简介
        VideoSynopsisBean bean = new VideoSynopsisBean();
        bean.setSubtitle(playerSubTitles.get(pos));
        bean.setTitle(playerTitles.get(pos));
        bean.setData("#搞笑 / " + playerDates.get(pos));
        items.add(new PlayerItem(PlayerItem.TYPE_0, bean));

        items.add(new PlayerItem(PlayerItem.TYPE_1, "相 关 视 频 推 荐"));
        //单个图片
        for (int i = 0; i < playerUrls.size(); i++) {
            VideoSynopsisBean bean1 = new VideoSynopsisBean();
            bean1.setId(i + "");
            bean1.setSubtitle(playerSubTitles.get(i));
            bean1.setTitle(playerTitles.get(i));
            bean1.setData("#搞笑 / " + playerDates.get(i));
            bean1.setImgUrl(playImgs.get(i));
            items.add(new PlayerItem(PlayerItem.TYPE_2, bean1));
        }
        return items;
    }

    private void initData() {
        String playImgl = "res:///" + R.drawable.player_list_2;
        String playImg2 = "res:///" + R.drawable.player_list_1;
        String playImg3 = "res:///" + R.drawable.player_list_3;
        String playImg4 = "res:///" + R.drawable.player_list_4;
        String playImg5 = "res:///" + R.drawable.player_list_5;
        playImgs.add(playImgl);
        playImgs.add(playImg2);
        playImgs.add(playImg3);
        playImgs.add(playImg4);
        playImgs.add(playImg5);


        String playUrl = "android.resource://" + getPackageName() + "/"
                + R.raw.video_5;
        String playUr2 = "android.resource://" + getPackageName() + "/"
                + R.raw.video_1;
        String playUr3 = "android.resource://" + getPackageName() + "/"
                + R.raw.video_2;
        String playUr4 = "android.resource://" + getPackageName() + "/"
                + R.raw.video_3;
        String playUr5 = "android.resource://" + getPackageName() + "/"
                + R.raw.video_4;
        playerUrls.add(playUrl);
        playerUrls.add(playUr2);
        playerUrls.add(playUr3);
        playerUrls.add(playUr4);
        playerUrls.add(playUr5);


        String playTitlel = "滑雪时你的面部表情";
        String playTitle2 = "小朋友吃超苦巧克力，表情萌到我了";
        String playTitle3 = "宝宝的冰雪初体验";
        String playTitle4 = "快来杯水，咖喱好辣 ";
        String playTitle5 = "拍摄直击之Spider-Man Skills";
        playerTitles.add(playTitlel);
        playerTitles.add(playTitle2);
        playerTitles.add(playTitle3);
        playerTitles.add(playTitle4);
        playerTitles.add(playTitle5);

        String playSubTitlel = "国外一位帅哥，在滑雪场上展示了滑雪时的面部表情，好逗";
        String playSubTitle2 = "各式各样的宝宝吃到超苦巧克力后的苦巴巴的表情，笑出内伤啊";
        String playSubTitle3 = "外国一爸爸让两个孩子体验滑雪，宝宝的反应超可爱";
        String playSubTitle4 = "日本咖喱广告";
        String playSubTitle5 = "蜘蛛侠扮演者汤姆·赫兰德为了蜘蛛侠高强度训练翻腾跳跃。";
        playerSubTitles.add(playSubTitlel);
        playerSubTitles.add(playSubTitle2);
        playerSubTitles.add(playSubTitle3);
        playerSubTitles.add(playSubTitle4);
        playerSubTitles.add(playSubTitle5);

        String playDatel = "20";
        String playDate2 = "19";
        String playDate3 = "19";
        String playDate4 = "15";
        String playDate5 = "15";
        playerDates.add(playDatel);
        playerDates.add(playDate2);
        playerDates.add(playDate3);
        playerDates.add(playDate4);
        playerDates.add(playDate5);


    }



    @Override
    public void onBackPressed() {
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onInteraction(int type, Object o) {
        switch (type) {
            case PlayerItem.TYPE_2:
                if (o instanceof VideoSynopsisBean) {
                    VideoSynopsisBean bean = (VideoSynopsisBean) o;
                    refreshData(bean.getId());
                }
                break;
        }
    }
}
